package com.sorhis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sorhis.dto.SysUserDTO;
import com.sorhis.entity.SysUser;
import com.sorhis.mapper.SysUserMapper;
import com.sorhis.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 账号表 服务实现类
 * </p>
 *
 * @author xihao.ding
 * @since 2025-02-08
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public IPage findPageList(SysUserDTO sysUserDTO) {
        Page<SysUser> page = new Page(sysUserDTO.getPageNum(), sysUserDTO.getPageSize());
        IPage<SysUser> result = this.page(page ,
                new QueryWrapper<SysUser>()
                        .like(StrUtil.isNotBlank( sysUserDTO.getIphone() ) , "iphone" , sysUserDTO.getIphone())
                        .like(StrUtil.isNotBlank( sysUserDTO.getLoginName() ) , "login_name" , sysUserDTO.getLoginName())
                        .like(StrUtil.isNotBlank( sysUserDTO.getUserName() ) , "user_name" , sysUserDTO.getUserName())
                        .eq(StrUtil.isNotBlank( sysUserDTO.getTenantId() ) , "tenant_id" , sysUserDTO.getTenantId())
                        .orderByDesc("create_time"));
        return result;
    }

    @Override
    @Transactional
    public Boolean saveOrUpateDomain(SysUserDTO sysUserDTO) {
        SysUser sysUser = new SysUser();
        BeanUtil.copyProperties(sysUserDTO , sysUser);
        return this.saveOrUpdate(sysUser);
    }

    @Override
    @Transactional
    public Boolean del(String id) {
        LambdaUpdateWrapper<SysUser> sysUserLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        Assert.notEmpty(id , "ID不能为空");
        sysUserLambdaUpdateWrapper.eq(SysUser::getId , id);
        sysUserLambdaUpdateWrapper.set(SysUser::getDeleted , 1);
        return this.update(sysUserLambdaUpdateWrapper);
    }
}
